/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class code2 {
    public boolean isValidBST(TreeNode root) {
        Stack<TreeNode> st = new Stack<>();
        long prev = Long.MIN_VALUE;

        while(!st.isEmpty() || root!=null) {
            //左边走到底
            while(root != null) {
                st.push(root);
                root = root.left;
            }
            //拿到节点
            root = st.pop();

            //判断
            if(root.val <= prev) {
                return false;
            }
            //更新值
            prev = root.val;
            //走右边
            root = root.right;
        }
        return true;
    }
}